package xhr;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
//异步消息发送
public class Producer {
    public static void main(String[] args) throws Exception {
        //创建生产者
        DefaultMQProducer producer = new DefaultMQProducer("xhr_producer");
        // 设置NameServer的地址
        producer.setNamesrvAddr("127.0.0.1:9876");
        //3.启动producer实例
        producer.start();
        //4.创建消息
        Message message = new Message("xhr_order", "pet", "超级柯基".getBytes(RemotingHelper.DEFAULT_CHARSET));

        //5.发送消息
        producer.send(message, new SendCallback() {
            public void onSuccess(SendResult sendResult) {
                System.out.println(sendResult);
            }

            public void onException(Throwable throwable) {
                throwable.printStackTrace();
                System.out.println("发送消息失败");
            }
        });
        //6.关闭
        producer.shutdown();
    }
}
